package com.example.hp.week12;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SetApplicationActivity extends AppCompatActivity {

    //定义 MyApplication 的对象，目标是获得应用程序的单一实例
    private MyApplication app;
    //定义用于交互的组件
    private EditText userNameEdt, orgNameEdt;
    private Button updateBnt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_application);
        //获得 MyApplication 对象 app
        app = (MyApplication) getApplication();
        //初始化组件
        userNameEdt = (EditText) findViewById(R.id.userNameEdt);
        orgNameEdt = (EditText) findViewById(R.id.orgNameEdt);
        updateBnt = (Button) findViewById(R.id.updateBnt);
        //利用 app 取出全局变量的值
        userNameEdt.setText(app.getUserName());
        orgNameEdt.setText(app.getOrgName());
        //占击按钮后，将修改全局变量，将来在别的 Activity 中将能看到修改后的结果
        // 本周实践中，后面的多个功能中将会用到这里设置好用户名称和机构名称
        updateBnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                app.setUserName(userNameEdt.getText().toString());

                app.setOrgName(orgNameEdt.getText().toString());
                finish();
            }
        });
    }
}
